package com.example.dict.GUI;

import com.example.dict.data.DictionaryStorage;
import com.example.dict.utils.Dictionary.Operations.AvlTree;
import com.example.dict.utils.Dictionary.dictionaryOperations;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import static com.example.dict.data.staticFilterData.WORD_CHECKER;


public class deleteGUI extends JFrame {

    dictionaryOperations dictionary = AvlTree.acquire();
    DictionaryStorage ds = new DictionaryStorage();
    JTextPane input;
    JButton delete;

    Color gray = Color.getHSBColor(0, 0, 0.949f);
    Color fontGray = Color.getHSBColor(0, 0, 0.7922f);

    int width = 320;
    int height = 230;

    public deleteGUI() {
        super("删除单词");

        JPanel panel = new JPanel();
        Border round = new LineBorder(gray, 25, true);
        input = new JTextPane();
        delete = new JButton("删除");

        panel.setBackground(Color.WHITE);
        panel.setLayout(null);

        input.setFont(new Font("楷体", Font.BOLD, 16));
        input.setForeground(fontGray);
        input.setText("删除单词");

        input.setBackground(gray);

        input.setBorder(round);

        panel.setBounds(0, 0, width, height);
        delete.setBounds(100, 140, 100, 30);
        input.setBounds(45, 20, 210, 110);

        setLayout(null);
        panel.add(delete);
        panel.add(input);
        add(panel);

        int x = ((Toolkit.getDefaultToolkit().getScreenSize()).width - width) / 2;
        int y = ((Toolkit.getDefaultToolkit().getScreenSize()).height - height) / 2;
        setLocation(x, y);
        setSize(width, height);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);
        setResizable(false);

        setVisible(true);

        delete.addActionListener(e -> {

            String word = input.getText();

            if (!word.matches(WORD_CHECKER)) {
                input.setForeground(fontGray);
                input.setText("请输入你要删除的单词");
                return;
            }

            word = word.toLowerCase();

            String translation = dictionary.translate(word);

            if (null == translation) {
                JOptionPane.showMessageDialog(null, "单词不存在");
                return;
            }

            dictionary.delete(word);
            System.out.println("删除成功");
            JOptionPane.showMessageDialog(null, "单词删除成功");
            try {
                ds.deleteRecord(word);
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
            input.setText("");
        });

        input.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                input.setText("");
                input.setForeground(Color.BLACK);
            }
        });
    }
}
